﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.ControlLib;
using xiaoxiong.KYJControllersLib;
using xiaoxiong.ScadaProModbusRtu.Common;

namespace xiaoxiong.ScadaProModbusRtu.childFrm
{
    public partial class FrmPumpControll : Form
    {
        public int index;
        public FrmPumpControll(int PumpIndex)
        {
            InitializeComponent();

            this.lbl_Title.Text = PumpIndex.ToString() + "号冷却泵监控";
            index = PumpIndex;

            //动态绑定
            this.textShow1.BindVarName = $"LQB{PumpIndex}_Current";
            this.textShow2.BindVarName = $"LQB{PumpIndex}_Speed";
            this.textShow3.BindVarName = $"LQB{PumpIndex}_TotalPower";
            this.textShow4.BindVarName = $"LQB{PumpIndex}_HeaterTemp";
            this.textShow5.BindVarName = $"LQB{PumpIndex}_Fre";
            this.textShow6.BindVarName = $"LQB{PumpIndex}_Power";
            this.textShow7.BindVarName = $"LQB{PumpIndex}__RunHour";
            //按钮绑定
            this.btn_Mode.Tag = $"LQB{PumpIndex}_Mode";
            this.btn_Start.Tag = $"LQB{PumpIndex}_Start";
            this.btn_Stop.Tag = $"LQB{PumpIndex}_Stop";
            //指示灯
            this.simpleLed1.BindVarName = $"LQB{PumpIndex}_RunState";
            this.simpleLed2.BindVarName = $"LQB{PumpIndex}_FaultState ";



            updateTimer = new Timer();
            updateTimer.Interval = 200;
            updateTimer.Tick += UpdateTimer_Tick;
            updateTimer.Start();

            this.FormClosing += (s, e) =>
            {
                updateTimer.Stop();
            };
        }


        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            UpdateCommon(this);

            //更新btn
            bool value = false;
            if (bool.TryParse(CommonMethods.plcDevice[this.btn_Mode.Tag?.ToString()]?.ToString(), out value))
            {
                btn_Mode.Text = value ? "自动模式" : "手动模式";
                btn_Start.Enabled = btn_Stop.Enabled = !value;
            }
        }


        private Timer updateTimer;

        /// <summary>
        /// 通用更新
        /// </summary>
        /// <param name="parentControl"></param>
        private void UpdateCommon(Control parentControl)
        {
            if (CommonMethods.plcDevice != null && CommonMethods.plcDevice.IsConnected)
            {
                foreach (Control control in parentControl.Controls)
                {
                    //显示textshow数据
                    if (control is TextShow textShow)
                    {
                        if (textShow.BindVarName.Length > 0)
                        {
                            float value = 0.0f;
                            if (float.TryParse(CommonMethods.plcDevice[textShow.BindVarName]?.ToString(), out value))
                            {
                                textShow.CurrentValue = value.ToString("f1");
                            }
                        }
                    }
                    //更新指示灯 状态
                    else if (control is SimpleLed led)
                    {
                        if (led.BindVarName.Length > 0)
                        {
                            bool value = false;
                            if (bool.TryParse(CommonMethods.plcDevice[led.BindVarName]?.ToString(), out value))
                            {
                                led.State = value;
                            }
                        }
                    }           
                    else if (control.HasChildren)
                    {
                        UpdateCommon(control);
                    }
                }
            }
        }



        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 无边框拖动

        //窗体拖动事件
        private bool _isDragging = false;
        private Point _startPoint = new Point(0, 0);
        private void TopPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _isDragging = true;
                _startPoint = new Point(e.X, e.Y);
            }
        }

        private void TopPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDragging)
            {
                Point newPoint = this.PointToScreen(new Point(e.X, e.Y));
                this.Location = new Point(newPoint.X - _startPoint.X, newPoint.Y - _startPoint.Y);
            }
        }

        private void TopPanel_MouseUp(object sender, MouseEventArgs e)
        {
            _isDragging = false;
        }
        #endregion


        #region 模式切换
        private void btn_Mode_Click(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (button.Tag != null &&  button.Tag.ToString().Length > 0)
                {
                    //取反写入
                    CommonMethods.CommonWrite(button.Tag.ToString(),(button.Text != "自动模式").ToString());
                }
            }
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {
            //信号
            if (sender is Button button)
            {
                if (button.Tag != null && button.Tag.ToString().Length > 0)
                {
                    CommonMethods.CommonWrite(button.Tag.ToString(),"True");
                    CommonMethods.CommonWrite($"LQB{index}_State", "1");
                    CommonMethods.CommonWrite($"LQB{index}_RunState", "True");
                }
            }
        }
        private void btn_Stop_Click(object sender, EventArgs e)
        {
            //信号
            if (sender is Button button)
            {
                if (button.Tag != null && button.Tag.ToString().Length > 0)
                {
                    CommonMethods.CommonWrite(button.Tag.ToString(), "false");
                    CommonMethods.CommonWrite($"LQB{index}_State", "0");
                    CommonMethods.CommonWrite($"LQB{index}_RunState", "false");
                }
            }
        }

        #endregion
    }
}
